package com.example.demo.test.alc;

import com.example.demo.socket.WebSocketServer;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.springframework.stereotype.Component;

import java.util.Random;
import static com.example.demo.test.selenium.SeleniumHelper.*;
@Component
public class MyProfile  implements BaseTest{
    
    public void test() throws Exception {
        WebSocketServer.screenInfoMsg("======================myProfileTest======================");
        WebSocketServer.screenInfoMsg("打开 MyProfile页面");
        waitAndClickByPath("//a[@href='/us/en/myprofile']");

        submitBaseInfo();
    }

    public void submitBaseInfo() throws InterruptedException {
        WebSocketServer.screenInfoMsg("生成 BaseInfo表单");
        int i = new Random().nextInt();
        String lastName = "xianwen" + i;
        wait.until(ExpectedConditions.presenceOfElementLocated(By.id("myprofile_titleCode")));

        findByIdAndSelectByIndex("myprofile_titleCode", 1);
        findByIdAndClearSend("myprofile_secondaryEmail", "978880162@qq.com");
        findByIdAndClearSend("myprofile_company", "lenovo");
        findByIdAndClearSend("myprofile_phoneNumber", "158000000000");
        findByIdAndSelectByIndex("myprofile_country", 1);
        findByIdAndSelectByIndex("myprofile_language", 1);
        findByIdAndClearSend("myprofile_postCode", "10000");
        findByIdAndSelectByIndex("myprofile_year", 1);
        findByIdAndSelectByIndex("myprofile_month", 1);
        findByIdAndSelectByIndex("myprofile_day", 1);
        findByIdAndClick("myprofile_subscriber_main");
        findByIdAndClick("myprofile_subscriber_second");
        findByIdAndClick("myprofile_subscriber_isSubscriber");
        findByIdAndClick("myprofile_subscriber_isAgreePrivacy");
        findByIdAndClearSend("myprofile_firstName", "zhang");
        findByIdAndClearSend("myprofile_lastName", lastName);

        WebSocketServer.screenInfoMsg("准备提交 BaseInfo表单");
        driver.findElement(By.xpath("//*[@id='tabContent']/div/div/div[2]/div[2]/div[2]/form/div[2]/span[2]/button")).click();
        Thread.sleep(2000);
        driver.navigate().refresh();
        wait.until(ExpectedConditions.presenceOfElementLocated(By.id("myprofile_lastName")));
        WebElement myprofile_lastName2 = driver.findElement(By.id("myprofile_lastName"));
        Thread.sleep(500);
        String text = myprofile_lastName2.getAttribute("value");
        if (lastName.equals(text)) {
            WebSocketServer.screenInfoMsg("MyProfile 保存成功");
        } else {
            WebSocketServer.screenInfoMsg("MyProfile 保存失败 lastName 与预期不一致 期望:%s 实际:%s", lastName, text);
        }
    }
}
